package com.qtjf.qtmoneyservice.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qtjf.qtmoneyservice.R;
import com.qtjf.qtmoneyservice.utils.FirstTip;
import com.qtjf.qtmoneyservice.view.YDOneView;
import com.qtjf.qtmoneyservice.view.YDThreeView;
import com.qtjf.qtmoneyservice.view.YDTwoView;

import java.util.ArrayList;
import java.util.List;

import me.relex.circleindicator.CircleIndicator;

public class YinDaoActivity extends AppCompatActivity {

    ViewPager viewPager;
    CircleIndicator indicator;
    MyViewpagerAdapter adapter;
    List<View> lists;
    TextView nextBt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_yin_dao);
        setTranslucent();
        initView();
        initData();
        initEvent();
    }

    private void initView() {
        viewPager= (ViewPager) findViewById(R.id.viewpager);
        indicator = (CircleIndicator)findViewById(R.id.indicator);
        nextBt= (TextView) findViewById(R.id.next_bt);
    }

    private void initData() {
        lists=new ArrayList<View>();
        lists.add(new YDOneView(this).getView());
        lists.add(new YDTwoView(this).getView());
        lists.add(new YDThreeView(this).getView());
        adapter=new MyViewpagerAdapter();
        viewPager.setAdapter(adapter);
        indicator.setViewPager(viewPager);

    }

    private void initEvent() {

        nextBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(YinDaoActivity.this,LoginActivity.class));
                YinDaoActivity.this.finish();
            }
        });

        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

                if(position==2)
                {
                    nextBt.setVisibility(View.VISIBLE);
                    indicator.setVisibility(View.GONE);
                }else
                {
                    nextBt.setVisibility(View.GONE);
                    indicator.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    class MyViewpagerAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return lists.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(lists.get(position));
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(lists.get(position));
            return lists.get(position);
        }
    }

  /*  @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus && Build.VERSION.SDK_INT >= 19) {
            View decorView = getWindow().getDecorView();
            decorView.setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
        }
    }*/

    private void addStatusBarView() {
        View view = new View(this);
        view.setBackgroundColor(Color.parseColor("#ffffff"));
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight());
        ViewGroup decorView = (ViewGroup) findViewById(android.R.id.content);
        decorView.addView(view, params);
    }

    public int getStatusBarHeight() {
        int result = 0;
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }

    public void setTranslucent() {//5.0
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
            addStatusBarView();
        }

        //4.4-5.0
        else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            WindowManager.LayoutParams local = getWindow().getAttributes();
            local.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | local.flags);
            addStatusBarView();
        }
    }
}
